/*
 * Copyright (c) 2017, www.zm0618.com All Rights Reserved..
 */

package com.light.hessian.remoting.client.balance;

import com.light.hessian.provider.slot.ProviderStatusSlotHolder;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by zhaojun on 2017/02/15.
 */
public class BalancerHolder {

    private final ConcurrentHashMap<String,BaseBalancer>  balancerMap=new ConcurrentHashMap<String, BaseBalancer>();

    public void registerBalancer(BaseBalancer balancer,ProviderStatusSlotHolder statusSlotHolder) {
        balancer.setStatusSlotHolder(statusSlotHolder);
        balancerMap.put(balancer.getBalancerName(),balancer);
    }
    public void refreshAllBalancers(){
        Set<Map.Entry<String, BaseBalancer>> entrySet = balancerMap.entrySet();
        if (CollectionUtils.isNotEmpty(entrySet)){
            for (Map.Entry<String, BaseBalancer> balancerEntry:entrySet) {
                BaseBalancer balancer=  balancerEntry.getValue();
                balancer.refreshSlots();
            }
        }
    }

    public BaseBalancer getBalancer(String name) {
        return balancerMap.get(name);
    }
}
